package com.gpc.hexapods.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

import com.gpc.hexapods.R;
import com.gpc.hexapods.data.SeekInfo;

import java.util.List;

public class AutoListAdapter extends BaseAdapter {

	private LayoutInflater mInflater;
	private AutoChangeListener mAutoChangeListener;
	private List<SeekInfo> mList;

	AutoListAdapter(Context context, AutoChangeListener listener, List<SeekInfo> list) {
		this.mInflater = LayoutInflater.from(context);
		this.mAutoChangeListener = listener;
		this.mList = list;
	}

	@Override
	public int getCount() {
		return mList.size();
	}

	@Override
	public Object getItem(int position) {
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		final ViewHolder holder;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.seekbar_list, parent, false);
			holder = new ViewHolder();
			holder.seekbar = (SeekBar) convertView.findViewById(R.id.seekbar);
			holder.seekbar.setProgress(mList.get(position).getValue());
			holder.name = (TextView) convertView.findViewById(R.id.name);
			holder.text = (TextView) convertView.findViewById(R.id.textview);
			holder.text.setText(mList.get(position).getValue() + "");
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		
		holder.name.setText(mList.get(position).getName());		
		holder.seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
			
			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
			}
			
			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
			}
			
			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				holder.text.setText(progress + "");
				mList.get(position).setValue(progress);
				mAutoChangeListener.autoListener(position + 1, progress);
				Log.d("zzzzzz", "name : " + (position + 1) + " value : " + progress);
			}
		});
		
		return convertView;
	}
	
	public class ViewHolder{
	    public SeekBar seekbar;
	    public TextView name;
	    public TextView text;
    }
	
	public interface AutoChangeListener {
		public void autoListener(int position, int value);
	}
}